var prefix="/user/";
$(function () {
	valid();
    layui.use('laydate', function(){
	  var laydate = layui.laydate;
	  laydate.render({
	    elem: '#birth' //指定元素
	  });
	});
});
$.validator.setDefaults({
    submitHandler: function() {
    	save();
    }
});
function save(){
	var d=getFormJson("signupForm");
	if (!Array.isArray(d.roleIds)) {
		var roleIdArr=[];
		roleIdArr.push(d.roleIds);
		d.roleIds=roleIdArr;
	}
    var config={
			url : prefix+"/save",
			data:d,
	}
	EasyAjax.post_json(config,function(r){
		layer.msg(r.msg);
		if (r.code == 0){
			parent.reLoad();
			var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
			parent.layer.close(index);
		} 
	});
	
}
function valid(){
    var icon = "<i class='fa fa-times-circle'></i>";
    $("#signupForm").validate({
        rules:{
        	username: {
        		 required: true,
                 maxlength: 10,
                 isStringAndNum:true
        	},
        	name: {
                required: true,
                maxlength: 5,
            },
            password: {
                required: true,
                maxlength:12,
                minlength:5
            },
            email: {
                required: true,
                maxlength: 20,
                email:true
            },
            mobile: {
                required: true,
                phoneZH:true,
            },
            birth: {
                required: true,
            },
            roleIds: {
                required: true,
            },
        },
        messages: {
        	username: {
        		required:icon + "请输用户账户",
        		maxlength:icon + "最多10个字符",
        		isStringAndNum:icon + "只能输入英文和数字",
        	},
        	name: {
        		required:icon + "请输用户名称",
        		maxlength:icon + "最多5个字符",
        	},
        	password: {
        		required:icon + "请输入密码",
        		maxlength:icon + "最多12个字符",
        		minlength:icon + "最少5个字符",
            },
            email: {
            	required:icon + "请输入邮箱",
            	email:icon + "最输入正确的邮箱格式",
            	maxlength: icon + "最多20位",
            },
            mobile: {
            	required:icon + "请输入角色名称",
            	phoneZH:icon + "请输入正确的手机号",
            },
            birth: {
            	required:icon + "请输出生日期",
            },
            roleIds: {
            	required:icon + "请选择角色名称",
            },
        }
    });
}

